package com.chenwenxuan.sync.ui.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.chenwenxuan.sync.event.Event;
import com.chenwenxuan.sync.event.EventsManager;
import com.chenwenxuan.sync.event.IEventHandler;
import com.chenwenxuan.sync.event.IEventSource;
import com.chenwenxuan.sync.ui.ImageIconResources;
import com.chenwenxuan.sync.ui.editor.desginer._FormEditor;
import com.javadocking.dock.TabDock;

import static com.chenwenxuan.sync.ui.ImageIconResources.*;

public abstract class FormEditor extends _FormEditor implements Editor {

	private static final long serialVersionUID = 1755666990519574984L;

	private boolean dirty;
	private TabDock parentDock;
	private String id;
	
	protected EventsManager eventManager;
	
	public FormEditor() {
		super();
		initUI();
		initEventManager();
		linkEvent();
		init();
	}

	protected void initUI() {
		getBtnModify().setIcon(ImageIconResources.getIconImage(ImageIconResources.ICON_FROM_ICON_EDIT));
		getBtnSave().setIcon(ImageIconResources.getIconImage(ImageIconResources.ICON_FROM_ICON_SAVE));
		getBtnCancel().setIcon(ImageIconResources.getIconImage(ImageIconResources.ICON_FROM_ICON_CANCEL));
	}
	
	protected void initEventManager() {
		dirty = false;
		eventManager = new EventsManager(this, new Event[]{EDITOR_CONTENT_CHANGED, EDITOR_CONTENT_SAVED});
	}
	
	protected void linkEvent() {
		getBtnModify().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				startEdit();
				
			}
		});
		
		getBtnSave().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				endEdit();
				raiseEditorContentSaved();
			}
		});
		
		getBtnCancel().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				endEdit();
			}
		});
	}
	
	public void init() {
		endEdit();
	}
	
	public void startEdit() {
		getBtnModify().setEnabled(false);
		getBtnSave().setEnabled(true);
		getBtnCancel().setEnabled(true);
	}
	
	public void endEdit() {
		getBtnModify().setEnabled(true);
		getBtnSave().setEnabled(false);
		getBtnCancel().setEnabled(false);
	}
	
	
	
	public void raiseEditorContentChanged() {
		setDirty(true);
		eventManager.raiseEvent(EDITOR_CONTENT_CHANGED.getName(), this);
	}
	
	public void raiseEditorContentSaved() {
		setDirty(false);
		eventManager.raiseEvent(EDITOR_CONTENT_SAVED.getName(), this);
	}


	@Override
	public boolean isDirty() {
		return dirty;
	}


	@Override
	public boolean save() {
		return false;
	}


	@Override
	public void setParentDock(TabDock parentDock) {
		this.parentDock = parentDock;
	}


	@Override
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public void addEventHandler(IEventHandler target, String event) {
		eventManager.addEventHandler(target, event);
	}

	@Override
	public void removeEventHandler(IEventHandler target, String event) {
		eventManager.addEventHandler(target, event);
	}

	@Override
	public String getId() {
		return this.id;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
}
